Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Jan 13, 2026

>>> from PIL import Image
>>> im = Image.new("L", (0, 0))
>>> im.save("out.png")
Traceback (most recent call last):
  File "PIL/ImageFile.py", line 660, in _save
    fh = fp.fileno()
AttributeError: '_idat' object has no attribute 'fileno'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 2592, in save
    save_handler(self, fp, filename)
  File "PIL/PngImagePlugin.py", line 1508, in _save
    ImageFile._save(
  File "PIL/ImageFile.py", line 664, in _save
    _encode_tile(im, fp, tile, bufsize, None, exc)
  File "PIL/ImageFile.py", line 682, in _encode_tile
    encoder.setimage(im.im, extents)
SystemError: tile cannot extend outside image

#9389 feels that this error is confusing. This PR takes the ValueError raised when saving a (0, 0) JPEG from #6159, and applies it generically when encoding images.

>>> from PIL import Image
>>> im = Image.new("L", (0, 0))
>>> im.save("out.png")
Traceback (most recent call last):
  File "PIL/ImageFile.py", line 660, in _save
    fh = fp.fileno()
AttributeError: '_idat' object has no attribute 'fileno'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 2592, in save
    save_handler(self, fp, filename)
  File "PIL/PngImagePlugin.py", line 1508, in _save
    ImageFile._save(
  File "PIL/ImageFile.py", line 664, in _save
    _encode_tile(im, fp, tile, bufsize, None, exc)
  File "PIL/ImageFile.py", line 682, in _encode_tile
    encoder.setimage(im.im, extents)
ValueError: cannot write empty image

This changed error specifically occurs when encoding images, not when saving images. SGI, ICNS and ICO formats are still able to save (0, 0) images with this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant